using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
namespace DalatCooking.Core
{
    class CategoryAccessor:ICategoryRepository
    {
        public override bool Insert(Category entity)
        {
            object rs = IDataProvider.Instance.OExecuteNonQuery("@CategoryId", "Category_Insert", entity.CategoryId, entity.Name, entity.Description, entity.Picture, entity.ParentId, entity.IsActive);
            return int.Parse(rs.ToString()) > 0;
        }
        public override bool Update(Category entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Category_Update",entity.CategoryId,entity.Name,entity.Description,entity.Picture,entity.ParentId,entity.IsActive,entity.SortIndex);
            return rs > 0;
        }
        public override bool Delete(int id)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Category_Delete", id) > 0;
        }
        public override bool Delete(Category entity)
        {
            return Delete(entity.CategoryId);
        }
        public override List<Category> FindAll()
        {
            return CBO.FillCollection<Category>(IDataProvider.Instance.ExecuteReader("Category_FindAll"));
        }
        public override Category FindById(int id)
        {
            return CBO.FillObject<Category>(IDataProvider.Instance.ExecuteReader("Category_FindById", id));
        }
        public override Category Single(int id)
        {
            return FindById(id);
        }
        public override Category FindByName(string name)
        {
            throw new NotImplementedException();
        }
        public override List<Category> PagingSql(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Category_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Category>(reader);
        }
        public override List<Category> PagingLinq(int pageSize, int pageNum, out int totalRowCount)
        {
             var query = from c in FindAll()
                    select c;
                    totalRowCount = FindAll().Count;
            var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
            return list;
        }
        public override bool SetActive(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Category_SetActive", id);
            return rs > 0;
        }
        public override bool SetSortIndex(int id, int isup)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Category_SetSortIndex", id, isup) > 0;
        }
        public override bool RemoveSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Category_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RemoveAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Category_RemoveAll");
            return rs > 0;
        }
        public override bool RestoreSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Category_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RestoreAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Category_RestoreAll");
            return rs > 0;
        }
    }
}